var props = require('./properties');
var gulp = require('gulp');
var nui = require('gulp-nui');
var htmlmin = require('gulp-htmlmin');
var nunjucks = require('gulp-nunjucks-render');
var webserver = require('gulp-webserver');
var path = require('path');
var proxy = require('http-proxy-middleware');//解决跨域
//静态资源根目录
var staticRoot = path.join(__dirname, '../');
//业务逻辑根目录
var staticUrl = path.join(__dirname, './src/');
//公共库资源目录
var staticLibs = path.join(__dirname, '../libs/');
//配置文件地址
var configurl = path.join(__dirname, './config.js');

//nui自动化参数
var options = {
	base:path.join(__dirname, '../'),
	url:configurl,
    ignore:['util', 'template', 'component', 'events', 'highcharts'],
    vermap:false,
	jsmin:{
		//validate.js 中的“$”符号混淆压缩之后，在IE中会报错。。。 fuck!
		mangle:{
			except:['$']
		} 
    },
    filterPath:function(url){
        return url.replace('{{staticUrl}}', staticRoot)
    }
}

//修改页面静态资源版本号，以及合并压缩模块
var pagerev = function(type){
	if(!type){
		delete options.mintype
	}
	else{
		options.mintype = type
	}
	return gulp.src(['./html/**/*.*'])
		.pipe(nui(options))
		.pipe(gulp.dest('./html/'))
}

gulp.task('pagerev', function(){
	delete options.compress;
	return pagerev()
})

gulp.task('pagerev_js', function(){
	return pagerev('js')
})

gulp.task('pagerev_css', function(){
	return pagerev('css')
})

gulp.task('dist', function(){
	gulp.src(['./html/**/*.html'])
    .pipe(nunjucks({
        path:'./html/tpl/',
        data:props
    }))
    .pipe(htmlmin({
        collapseWhitespace:true,
        removeComments:true,
        removeEmptyAttributes:true
    }))
    .pipe(gulp.dest('./dist/'))
})

//修改js模块中资源版本号
gulp.task('jsrev', function(){
	gulp.src([staticUrl+'**/*.js', '!'+ staticUrl +'**/*-min.js'])
	     .pipe(nui({
			 filterPath:function(src){
				return src.replace('<%staticLibs%>', staticLibs).replace('<%staticUrl%>', staticUrl)
			 }
		 }))
	     .pipe(gulp.dest(staticUrl))
})

//修改业务模块css中资源版本号
gulp.task('cssrev', function(){
	gulp.src([staticUrl+'**/*.{css,less}', '!'+ staticUrl +'**/*{-min}.css'])
	     .pipe(nui())
	     .pipe(gulp.dest(staticUrl))
})

//修改公共库css中的资源版本号
gulp.task('libsCssrev', function(){
	gulp.src([staticLibs+'**/*.{css,less}', '!'+ staticLibs +'**/*{-min}.css'])
	     .pipe(nui())
	     .pipe(gulp.dest(staticLibs))
})

gulp.task('webserver', function() {
    gulp.src('../')
        .pipe(webserver({
            livereload:true,
            open:false,
            port:'8089',
        }));
});

gulp.task('watch', function(){
    
    if(props.environment === 'dev'){
        gulp.watch([
            'src/**/*.js', 
            staticLibs+'**/*.js',
            './properties.js',
            './config.js',
            '!src/**/*-min.js'
        ], ['pagerev_js']);

        gulp.watch([
            'src/**/*.{css,less}', 
            staticLibs+'**/*.{css,less}', 
            '!src/**/*-min.css',
        ], ['pagerev_css']);

        var pic = '{jpg,png,gif,eot,svg,ttf,woff}';

        gulp.watch(['src/**/*.'+pic, staticLibs+'**/*.'+pic], ['jsrev']);

        gulp.watch(['src/**/*.'+pic], ['cssrev']);
        
        gulp.watch([staticLibs+'**/*.'+pic], ['libsCssrev']);

    }

    gulp.watch(['html/**/*.*'], ['dist']);
});

if(props.environment === 'dev'){
    gulp.task('default', ['pagerev', 'jsrev', 'cssrev', 'libsCssrev', 'dist', 'watch','webserver']);
}
else{
    gulp.task('default', ['dist', 'watch']);
}